<%@page contentType="content-type: application/x-javascript" pageEncoding="UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
var MIS_DATOS = {
	
	inicio : function() {
		var contenedor = $('#mis-datos');
		contenedor.find('form').reset();
		$('#datos-contrasenya .cambiar').click(function() {
			contenedor.find('.ampliar').fadeIn(VELOCIDAD_FADE);
			$('#contrasenya-actual').focus();
		});
		$('#avatar').remove();
		var caja = $('#datos-juego fieldset');
		caja.find('button').remove();
		caja.append('<button id="boton-elegir-imagen" type="button" title="Cambiar la imagen asociada a mi cuenta"><span>Cambiar imagen</span></button>');
		$('#boton-elegir-imagen').before('<div id="progreso-fichero"></div>');
		$('#progreso-fichero').css('visibility','hidden');
		var swfu = new SWFUpload({
			// Backend Settings
			upload_url: "/upload/avatar/",
			// File Upload Settings
			file_size_limit : "1 MB",	// 2MB
			file_types : "*.gif;*.jpg;*.png",
			file_types_description : "Archivos de imÃ¡genes",
			file_upload_limit : "0",
			// Event Handler Settings - these functions as defined in Handlers.js
			//  The handlers are not part of SWFUpload but are part of my website and control how
			//  my website reacts to the SWFUpload events.
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			// Button Settings
			button_placeholder_id : "boton-elegir-imagen",
			button_image_url: "/img/css/<s:text name="langFlag" />/botonEligeImagen.png",
			button_width: 141,
			button_height: 25,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,
			// Flash Settings
			flash_url : "/swf/swfupload.swf",
			custom_settings : {
				upload_target : "progreso-fichero"
			},
			// Debug Settings
			debug: false,
			// Post params
     		post_params : { "fileJsessionid" : "<%=request.getSession().getId()%>"}
		});
		contenedor.find('.ampliar form').submit(function() {
			CARGANDO.mostrar(contenedor.find('.informacion'));
			$.ajax({
				type: 'POST',
				url: '/xhr/xhr_cambiar_contrasenya',
				data: 'contrasenaActualSha1=' + SHA1($('#contrasenya-actual').val()) + '&contrasenaSha1=' + SHA1($('#nueva-contrasenya').val()) + '&contrasenaMd5=' + hex_md5($('#nueva-contrasenya').val()),
				dataType: 'json',
				success: function(datos) {
					if (datos['cabecera']['correcto'] != '1')
						AVISO.mostrar(MENSAJE_ERROR_AJAX,1,0);
					CARGANDO.ocultar(contenedor.find('div.cargando'));
					MIS_DATOS.ocultar();
				},
				error: function() {
					AVISO.mostrar(MENSAJE_ERROR_AJAX,1,0);
					CARGANDO.ocultar(contenedor.find('div.cargando'));
					MIS_DATOS.ocultar();
				}
			});
			return false;
		});
		contenedor.find('.ampliar .boton-cerrar').click(function() {
			MIS_DATOS.ocultar();
		});
	},
	
	ocultar : function() {
		$('#mis-datos .ampliar').fadeOut(VELOCIDAD_FADE);
	}
	
}




$(document).ready(function() {
	MIS_DATOS.inicio();
	$(document).keyup(function (e) {
		if (typeof(e) == 'undefined')
			e = event;
		if (e.keyCode == 27) {
			MIS_DATOS.ocultar();
		}
	});
});